package com.bluesky.objectworld;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.management.RuntimeErrorException;


public class ObjectImpl implements _Object {
	long id;

	String nativeClassName;
	
	Map<String, Attribute> attributes;
	Map<String, _Method> methods;
	
	public ObjectImpl() {
		attributes = new HashMap<String, Attribute>();
		methods = new HashMap<String, _Method>();
	}

	@Override
	public long getId() {
		return id;
	}

	@Override
	public void addAttribute(String type, String name) {
		attributes.put(name, new Attribute(type, name));

	}

	@Override
	public void removeAttribute(String name) {
		attributes.remove(name);

	}

	@Override
	public Collection<Attribute> getAttributes() {
		return attributes.values();
	}

	@Override
	public Object getAttributeValue(String name) {
		return attributes.get(name).value;

	}

	@Override
	public void setAttributeValue(String name, Object value) {
		attributes.get(name).value = value;

	}

	@Override
	public void addMethod(String name, _Method method) {
		methods.put(name, method);
	}

	@Override
	public void removeMethod(String name) {
		methods.remove(name);
	}

	@Override
	public Collection<_Method> getMethods() {

		return methods.values();
	}

	@Override
	public _Object processMessage(String name, _Object... parameters) {
		
		_Method method = methods.get(name);
		if(method==null)
			throw new RuntimeException("message do not understand:" + name);
		
		return method.execute(parameters);		
	}
	 
	@Override
	public String toString() {

		return String.format("#%d$%s", id, getClass().getSimpleName());
	}
}
